package com.jrtc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.constants.PublicConstants;
import com.jrtc.base.entity.bo.ArticulusReport;
import com.jrtc.base.entity.bo.Exerciserecord;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.dao.ArticulusReportDao;
import com.jrtc.base.entity.vo.ArticulusReportVo;
import com.jrtc.base.entity.vo.IdNameInfoVo;
import com.jrtc.dao.ExerciserecordDao;
import com.jrtc.dao.PatientDao;
import com.jrtc.service.ArticulusReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  关节活动度报告(ArticulusReport)表服务实现类
 *
 * @author makejava
 * @since 2022-09-07 15:52:18
 */
@Service("articulusReportService")
public class ArticulusReportServiceImpl implements ArticulusReportService
{
    @Resource
    private ArticulusReportDao articulusReportDao;
    @Resource
    private PatientDao patientDao;
    @Resource
    private ExerciserecordDao exerciserecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ArticulusReportVo queryById(Integer id)
    {
        ArticulusReport articulusReport = this.articulusReportDao.queryById(id);
        ArticulusReportVo articulusReportVo = new ArticulusReportVo();
        articulusReportVo.setBloodArea(JSON.parseArray(articulusReport.getBloodArea(), IdNameInfoVo.class));
        articulusReportVo.setBloodSaturation(JSON.parseArray(articulusReport.getBloodSaturation(),IdNameInfoVo.class));
        articulusReportVo.setHeart(JSON.parseArray(articulusReport.getHeart(),IdNameInfoVo.class));
        articulusReportVo.setHrv(JSON.parseArray(articulusReport.getHrv(),IdNameInfoVo.class));
        articulusReportVo.setHighRisk(JSON.parseArray(articulusReport.getHighRisk(),IdNameInfoVo.class));
        articulusReportVo.setLowRisk(JSON.parseArray(articulusReport.getLowRisk(),IdNameInfoVo.class));
        return articulusReportVo;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ArticulusReport> queryAllByLimit(int offset, int limit) {
        return this.articulusReportDao.queryAllByLimit(offset, limit);
    }

    @Override
    public IPage<ArticulusReport> queryArticulusReportList(ArticulusReport articulusReport)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ArticulusReport> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(articulusReport.getPageNo(), articulusReport.getPageSize());
        IPage<ArticulusReport> articulusReportPage = this.articulusReportDao.queryArticulusReportList(page, articulusReport);
        return articulusReportPage;
    }
    /**
     * 新增数据
     *
     * @param articulusReport 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(ArticulusReportVo articulusReport) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Patient patient = patientDao.needBallCount(articulusReport.getIdCard());
        if (patient == null){
            throw new Exception("证件号无记录");
        }

        ArticulusReport tmpArticulusReport = new ArticulusReport();
        tmpArticulusReport.setBloodArea("{\"bloodArea\":" + JSON.toJSONString(articulusReport.getBloodArea()) + "}");
        tmpArticulusReport.setBloodSaturation("{\"bloodSaturation\":" + JSON.toJSONString(articulusReport.getBloodSaturation()) + "}");
        tmpArticulusReport.setHeart("{\"heart\":" + JSON.toJSONString(articulusReport.getHeart()) + "}");
        tmpArticulusReport.setHrv("{\"hrv\":" + JSON.toJSONString(articulusReport.getHrv()) + "}");
        tmpArticulusReport.setHighRisk("{\"highRisk\":" + JSON.toJSONString(articulusReport.getHighRisk()) + "}");
        tmpArticulusReport.setLowRisk("{\"lowRisk\":" + JSON.toJSONString(articulusReport.getLowRisk()) + "}");
        int insert = this.articulusReportDao.insert(tmpArticulusReport);
        if (insert <= 0){
            throw new Exception("添加关节活动记录失败");
        }

        Exerciserecord exerciserecord = new Exerciserecord();
        exerciserecord.setPatientId(patient.getPatientId());
        exerciserecord.setDuration(articulusReport.getDuration());
        exerciserecord.setDate(format.format(new Date()));
        exerciserecord.setActionType(PublicConstants.RecordEnum.Joint.getValue());
        exerciserecord.setActionTypeId(tmpArticulusReport.getId());
        exerciserecord.setPrescriptionName("关节活动度");
        insert = exerciserecordDao.insertExerciserecord(exerciserecord);
        if (insert <= 0){
            throw new Exception("添加患者运动总记录失败");
        }

        return insert;
    }

    /**
     * 修改数据
     *
     * @param articulusReport 实例对象
     * @return 实例对象
     */
    @Override
    public ArticulusReport update(ArticulusReport articulusReport) {
        this.articulusReportDao.update(articulusReport);
        return this.articulusReportDao.queryById(articulusReport.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.articulusReportDao.deleteById(id) > 0;
    }

    /**
     * 关节活动度图表
     * */
    @Override
    public ArticulusReportVo articulusReport(Integer id) {
        ArticulusReportVo articulusReportVo = new ArticulusReportVo();
        ArticulusReport articulusReport = articulusReportDao.queryById(id);
        if (articulusReport != null){
            JSONObject jsonHighRisk = JSONObject.parseObject(articulusReport.getHighRisk());
            String versionHighRisk = jsonHighRisk.getString("highRisk");
            List<IdNameInfoVo> highRiskList = JSON.parseArray(versionHighRisk, IdNameInfoVo.class);
            articulusReportVo.setHighRisk(highRiskList);

            JSONObject jsonLowRisk = JSONObject.parseObject(articulusReport.getLowRisk());
            String versionLowRisk = jsonLowRisk.getString("lowRisk");
            List<IdNameInfoVo> lowRiskList = JSON.parseArray(versionLowRisk, IdNameInfoVo.class);
            articulusReportVo.setLowRisk(lowRiskList);

            JSONObject jsonHeart = JSONObject.parseObject(articulusReport.getHeart());
            String versionHeart = jsonHeart.getString("heart");
            List<IdNameInfoVo> heartList = JSON.parseArray(versionHeart, IdNameInfoVo.class);
            if (heartList != null && heartList.size() > 0){
                heartList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
                for (IdNameInfoVo idNameInfoVo : heartList) {
                    idNameInfoVo.setHighestValue(heartList.get(heartList.size() - 1).getValue());
                    idNameInfoVo.setLowestValue(heartList.get(0).getValue());
                }
            }
            articulusReportVo.setHeart(heartList);

            JSONObject jsonBloodSaturation = JSONObject.parseObject(articulusReport.getBloodSaturation());
            String versionBloodSaturation = jsonBloodSaturation.getString("bloodSaturation");
            List<IdNameInfoVo> bloodSaturationList = JSON.parseArray(versionBloodSaturation, IdNameInfoVo.class);
            if (bloodSaturationList != null && bloodSaturationList.size() > 0){
                bloodSaturationList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
                for (IdNameInfoVo idNameInfoVo : bloodSaturationList) {
                    idNameInfoVo.setHighestValue(bloodSaturationList.get(bloodSaturationList.size() - 1).getValue());
                    idNameInfoVo.setLowestValue(bloodSaturationList.get(0).getValue());
                }
            }
            articulusReportVo.setBloodSaturation(bloodSaturationList);

            JSONObject jsonHrv = JSONObject.parseObject(articulusReport.getHrv());
            String versionHrv = jsonHrv.getString("hrv");
            List<IdNameInfoVo> hrvList = JSON.parseArray(versionHrv, IdNameInfoVo.class);
            if (hrvList != null && hrvList.size() > 0){
                for (IdNameInfoVo idNameInfoVo : hrvList) {
                    idNameInfoVo.setHighestValue(hrvList.get(hrvList.size() - 1).getValue());
                    idNameInfoVo.setLowestValue(hrvList.get(0).getValue());
                }
            }
            articulusReportVo.setHrv(hrvList);

            JSONObject jsonBloodArea = JSONObject.parseObject(articulusReport.getBloodArea());
            String versionBloodArea = jsonBloodArea.getString("bloodArea");
            List<IdNameInfoVo> bloodAreaList = JSON.parseArray(versionBloodArea, IdNameInfoVo.class);
            if (bloodAreaList != null && bloodAreaList.size() > 0){
                for (IdNameInfoVo idNameInfoVo : bloodAreaList) {
                    idNameInfoVo.setHighestValue(bloodAreaList.get(bloodAreaList.size() - 1).getValue());
                    idNameInfoVo.setLowestValue(bloodAreaList.get(0).getValue());
                }
            }
            articulusReportVo.setBloodArea(bloodAreaList);
        }
        return articulusReportVo;
    }
}